<?xml version="1.0" encoding="UTF-8" ?>
<#assign en = 0/>
<#assign dto = 8/>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${ftl.packageName}.${cgH.moduleName}.${tc.entityName}${tc.entityPostfix}" >
    <resultMap id="BaseResultMap" type="${mList[dto].packageName}.${tc.moduleName}.${tc.entityName}${mList[dto].classPostfix}" >
        <id column="ID" property="id" jdbcType="VARCHAR" />
    <#list tc.templateFieldConfigList as po>
     <#if "${po.name}" !="id">
      <#if "${po.type}" == "String">
         <result column="${po.name ? upper_case}" property="${po.fieldName}" jdbcType="VARCHAR" />
      </#if>
      <#if "${po.type}" == "Long">
          <result column="${po.name ? upper_case}" property="${po.fieldName}" jdbcType="BIGINT" />
      </#if>
     <#if "${po.type}" == "Date">
         <result column="${po.name ? upper_case}" property="${po.fieldName}" jdbcType="TIMESTAMP" />
     </#if>
     <#if "${po.type}" == "Integer" >
         <result column="${po.name ? upper_case}" property="${po.fieldName}" jdbcType="INTEGER" />
     </#if>
     </#if>
    </#list>
    </resultMap>

    <!-- 分页查询 -->
    <select id="pageQuery${tc.entityName}" parameterType="com.stars.entity.ListPage" resultMap="BaseResultMap">
        SELECT
            <include refid="SELECT_FIELD_LIST"></include>
        FROM  ${tc.tableName ? upper_case} t
        <where>
             1=1
            <include refid="select_page_where"></include>
        </where>

    </select>

<#if "${cgH.isTree =='1'}">
    <!-- 树形查询 #TODO .* 问题-->
    <select id="findByTreeList" parameterType="${mList[dto].packageName}.${tc.moduleName}.${tc.entityName}${mList[dto].classPostfix}" resultMap="BaseResultMap">
        SELECT
            <include refid="SELECT_FIELD_LIST"></include>
        FROM ${tc.tableName ? upper_case} t
        WhERE 1=1
        <include refid="select_page_where"/>
        ORDER BY t.LAYER,t.SORT_NUM
    </select>

    <select id="findByPid" parameterType="java.lang.String" resultMap="BaseResultMap">
        SELECT
        <include refid="SELECT_FIELD_LIST"></include>
        FROM ${tc.tableName ? upper_case} t
        WHERE t.pid = ${r"#{"}pid${r"}"}
        ORDER BY t.LAYER,t.SORT_NUM
    </select>

    <select id="findTreeList" parameterType="java.util.List" resultMap="BaseResultMap">
        <include refid="sql_findTree"></include>
    </select>
    <sql id="sql_findTree">
        SELECT
            <include refid="SELECT_FIELD_LIST"></include>
        FROM ${tc.tableName ? upper_case} t WHERE 1=1
        <if test="list!=null and list.size>0">
            AND
            <foreach collection="list" index="index" item="item" open="(" separator="OR" close=")">
                FIND_IN_SET(id, ${tc.tableName? upper_case}_GET_PARENT(${r"#{"}item.id,jdbcType=VARCHAR${r"}"})) OR FIND_IN_SET(id,
               ${tc.tableName? upper_case}_GET_CHILD(${r"#{"}item.id,jdbcType=VARCHAR${r"}"}))
            </foreach>
        </if>
        ORDER BY t.`layer`, t.`sort_num`
    </sql>
</#if>

    <!-- 查询条件sql -->
    <sql id="select_page_where">
        <if test="entity != null">
        <#list tc.templateFieldConfigList as p>
         <#if "${p.isQuery}" == "1">
             <if test="entity.${p.fieldName} != null and entity.${p.fieldName} != '' ">
             <#if "${p.operate}" =="like">
                 AND t.${p.name ? upper_case} LIKE CONCAT('%',${r"#{"}entity.${p.fieldName}${r"}"},'%')
             <#else >
                 AND t.${p.name ? upper_case} ${p.operate} ${r"#{"}entity.${p.fieldName}${r"}"}
             </#if>
             </if>
         </#if>
        </#list>

            <#if "${cgH.isCreateAppOrg}" =="2">
                <if test="entity.createId != null and entity.createId != '' ">
                    AND t.CREATE_ID =${r"#{"}entity.createId${r"}"}
                </if>
                <!-- 查询组织公司 -->
                <if test="entity.companyId != null and entity.companyId != '' ">
                    AND t.COMPANY_ID =${r"#{"}entity.companyId${r"}"}
                </if>
                <if test="entity.companyCode != null and entity.companyCode != '' ">
                    AND t.COMPANY_CODE =${r"#{"}entity.companyCode${r"}"}
                </if>
            <#elseif "${cgH.isCreateAppOrg}" =="3">
                <if test="entity.orgId != null and entity.orgId != '' ">
                    AND t.ORG_ID =${r"#{"}entity.orgId${r"}"}
                </if>
                <if test="entity.orgCode != null and entity.orgCode != '' ">
                    AND t.ORG_CODE =${r"#{"}entity.orgCode${r"}"}
                </if>
                <if test="entity.companyId != null and entity.companyId != '' ">
                    AND t.COMPANY_ID =${r"#{"}entity.companyId${r"}"}
                </if>
            <#elseif "${cgH.isCreateAppOrg}" =="4">
                <if test="entity.appId != null and entity.appId != '' ">
                    AND t.APP_ID =${r"#{"}entity.appId${r"}"}
                </if>
                <if test="entity.companyId != null and entity.companyId != '' ">
                    AND t.COMPANY_ID =${r"#{"}entity.companyId${r"}"}
                </if>
                <if test="entity.orgId != null and entity.orgId != '' ">
                    AND t.ORG_ID =${r"#{"}entity.orgId${r"}"}
                </if>
            </#if>
        </if>
    </sql>
    <!-- 查询字段 -->
    <sql id="SELECT_FIELD_LIST">
        t.ID as rowId
    <#list tc.templateFieldConfigList as po>
       ,t.${po.name ? upper_case}
    </#list>
    </sql>

</mapper>